package org.grill.fatwhacker.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.grill.fatwhacker.FatWhacker;
import org.grill.fatwhacker.data.FoodWeek;
import org.grill.fatwhacker.data.WeekSet;
import org.grill.fatwhacker.tools.PointFinderDialog;
import org.joda.time.LocalDate;

public class MainWindow {

	Display d;
	Shell s;
	private SashForm sashForm;
	
	private WeekSet weekSet = new WeekSet();
	
	public MainWindow(Display d) {
		this.d = d;
	}
	

	public void setWeekSet(WeekSet weekSet) {
		this.weekSet = weekSet;
	}

	public void open() {
		s = new Shell(d);
//		s.setImage(new Image(d, "fw.png"));
		s.setText("FatWhacker " + FatWhacker.version);
//		GridLayout layout = new GridLayout();
//		layout.numColumns =1;
//		layout.horizontalSpacing = 5;
//		layout.verticalSpacing = 5;
//		s.setLayout(layout);
		s.setLayout(new FillLayout());
		
		setupMenu();
		
		

		
		sashForm = new SashForm(s, SWT.HORIZONTAL);
		sashForm.setLayout(new FillLayout());
		
		Composite weekComposite = new Composite(sashForm, SWT.NONE);
		weekComposite.setLayout(new FillLayout());
		weekView = new WeekSelectionView(weekComposite);
		weekView.setWeekSet(weekSet);
		weekView.setCurrentWeek(weekSet.getFoodWeek(new LocalDate()));
		weekView.render();
		
		weekView.addListener(new Listener() {
			public void handleEvent(Event arg0) {
				FoodWeek currentWeek = weekView.getCurrentWeek();
				weekDetailView.setWeek(currentWeek);
				weekDetailView.render();
				

				dayDetailView.setDay(currentWeek.getFirst());
				dayDetailView.render();
				
			}
		});
		
		Composite weekViewComposite = new Composite(sashForm, SWT.NONE);
		weekDetailView = new WeekDetailView(weekViewComposite);
		weekDetailView.setWeek(weekSet.getFoodWeek(new LocalDate()));
		weekDetailView.render();
//		weekViewComposite.setLayout(new FillLayout());
		
		
		Composite dayComposite = new Composite(sashForm, SWT.NONE);
		dayComposite.setLayout(new FillLayout(SWT.VERTICAL));

		dayDetailView = new DayDetailView(dayComposite);
		dayDetailView.setDay(weekSet.getFoodWeek(new LocalDate()).getDay(new LocalDate()));
		dayDetailView.render();
		dayDetailView.addChangeListener(new Listener() {
			public void handleEvent(Event arg0) {
				weekDetailView.render();
			}
		});
		
		weekDetailView.setDayDetailView(dayDetailView);
		
		sashForm.setWeights(new int[] {30, 30, 45});
		
		s.setSize(800,600);
		
		s.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				System.out.println("Exiting");
				weekSet.save();
			}
		});
		
		d.timerExec(5*60*1000, new Runnable() {
			public void run() {
				weekSet.save();
				d.timerExec(5*60*1000, this);
			}
		});
		
		s.open();
		
	}


	private void setupMenu() {
		Menu mainMenu = new Menu(s, SWT.BAR);
		s.setMenuBar(mainMenu);
		

		MenuItem toolsItem = new MenuItem(mainMenu, SWT.CASCADE);
		toolsItem.setText("Tools");
		Menu toolsMenu = new Menu(mainMenu);
		toolsItem.setMenu(toolsMenu);
		
		MenuItem pfItem = new MenuItem(toolsMenu, SWT.PUSH);
		pfItem.setText("PointsFinder");
		pfItem.addListener(SWT.Selection, new Listener() {
		
			public void handleEvent(Event arg0) {
				new PointFinderDialog(s).open();
			}
		
		});
		
	}

	WeekDetailView weekDetailView;
	WeekSelectionView weekView;
	DayDetailView dayDetailView;
}
